/**
 * 员工计件录入相关API接口
 */

const { post } = require('./request.js');

/**
 * 获取产品工序列表
 * @returns {Promise<Object>} API响应
 */
const getProductProcessList = async () => {
  try {
    const response = await post('/workEntry/processList');
    
    
    if (response.code === 200) {
      return {
        success: true,
        data: response.data,
        message: response.message || '获取成功'
      };
    } else {
      return {
        success: false,
        message: response.message || '获取产品工序列表失败'
      };
    }
  } catch (error) {

    return {
      success: false,
      message: error.message || '网络请求失败'
    };
  }
};

/**
 * 提交计件录入
 * @param {Object} entryData 录入数据
 * @param {string} entryData.productCode 产品编码
 * @param {string} entryData.productName 产品名称
 * @param {string} entryData.processCode 工序号
 * @param {string} entryData.processName 工序名称
 * @param {string} entryData.quantity 数量
 * @param {string} entryData.price 单价
 * @param {string} entryData.employeeNumber 工号
 * @param {string} entryData.employeeName 姓名
 * @returns {Promise<Object>} API响应
 */
const submitWorkEntry = async (entryData) => {
  try {
    
    // 确保所有参数都是string类型
    const requestData = {
      productCode: String(entryData.productCode || ''),
      productName: String(entryData.productName || ''),
      processCode: String(entryData.processCode || ''),
      processName: String(entryData.processName || ''),
      quantity: String(entryData.quantity || ''),
      price: String(entryData.price || ''),
      employeeNumber: String(entryData.employeeNumber || ''),
      employeeName: String(entryData.employeeName || '')
    };
    
    
    const response = await post('/workEntry/entry', requestData);
    
    
    if (response.code === 200) {
      return {
        success: true,
        data: response.data,
        message: response.message || '录入成功'
      };
    } else {
      return {
        success: false,
        message: response.message || '录入失败'
      };
    }
  } catch (error) {

    return {
      success: false,
      message: error.message || '网络请求失败'
    };
  }
};

/**
 * 获取今日录入列表
 * @param {string} date 查询日期 yyyy-MM-dd
 * @param {string} account 员工工号
 * @returns {Promise<Object>} API响应
 */
const getTodayEntryList = async (date, account) => {
  try {
    
    // 手动构建查询参数字符串（微信小程序不支持URLSearchParams）
    const params = [];
    if (date) {
      params.push(`date=${encodeURIComponent(String(date))}`);
    }
    if (account) {
      params.push(`account=${encodeURIComponent(String(account))}`);
    }
    const queryString = params.length > 0 ? `?${params.join('&')}` : '';
    
    
    const response = await post(`/checkSalary/salaryDayList${queryString}`, {});
    
    
    if (response.code === 200 || response.code === 0) {
      return {
        success: true,
        data: response.data,
        message: response.message || '获取成功'
      };
    } else {
      return {
        success: false,
        message: response.message || '获取今日录入列表失败'
      };
    }
  } catch (error) {

    return {
      success: false,
      message: error.message || '网络请求失败'
    };
  }
};

module.exports = {
  getProductProcessList,
  submitWorkEntry,
  getTodayEntryList
}; 